package com.nl.list;

import com.nl.list.LinkedList.Node;

public class ListMidNode {

	// 快慢指针找到链表中间节点
	public static <T> Node<T> midNode(LinkedList<T> list) {

		if (list != null && list.phead != null) {
			Node<T> first = list.phead, second = list.phead;
			while (second.next!=null &&second.next.next != null) {
				second = second.next.next;
				first = first.next;
			}
			return first;
		} else {
			return null;
		}

	}

	//快慢指针解决是否是循环链表
	public static <T> boolean isCircleLinkedList(LinkedList<T> list) {

		if (list == null || list.phead == null) {
			return false;
		}
		Node<T> first = list.phead, second = list.phead;
		while (second.next.next != null) {
			second = second.next.next;
			first = first.next;
			if (second == first) {
				return true;
			}
		}
		return false;
	}
}
